import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/components/ye_xiu/login/text_field.dart';
import 'package:flutter_eve_plugin/pages/ye_xiu/login/actions.dart';
import 'package:flutter_eve_plugin/store/main.dart';
import 'package:flutter_eve_plugin/store_vm/app_vm.dart';
import 'package:flutter_eve_plugin/store_vm/ye_xiu/login/main.dart';
import 'package:flutter_eve_plugin/store_vm/ye_xiu/main.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';
import 'package:flutter_eve_plugin/constants/icon_font.dart';
import 'package:flutter_redux/flutter_redux.dart';

class ShowDateWidget extends StatefulWidget {
  @override
  _ShowDateWidgetState createState() => _ShowDateWidgetState();
}

class _ShowDateWidgetState extends State<ShowDateWidget> {
  DateTime date = DateTime.parse('1994-06-15');

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, AppStateViewModel>(
        onInit: (store) {
          inputBirthday(store, date.millisecondsSinceEpoch);
        },
        converter: (store) => AppStateViewModel(
            state: store.state,
            yeXiuStateViewModel: YeXiuStateViewModel(loginStateViewModel:
                LoginStateViewModel(inputBirthday: (birthday) {
              inputBirthday(store, birthday);
            }))),
        builder: (context, vm) {
          return GestureDetector(
            onTap: () {
              showCupertinoModalPopup<void>(
                context: context,
                builder: (BuildContext context) {
                  return _buildBottomPicker(
                    CupertinoDatePicker(
                      mode: CupertinoDatePickerMode.date,
                      initialDateTime: date,
                      onDateTimeChanged: (DateTime newDateTime) {
                        if (mounted) {
                          setState(() {
                            date = newDateTime;
                          });
                          vm.yeXiuStateViewModel.loginStateViewModel
                              .inputBirthday(date.millisecondsSinceEpoch);
                        }
                      },
                    ),
                  );
                },
              );
            },
            child: Container(
              margin: EdgeInsets.only(left: setRatio(29), right: setRatio(29)),
              decoration: ShapeDecoration(
                  shape: UnderlineInputBorder(
                      borderSide: BorderSide(
                          color: Color(0xFFE5E5E5),
                          style: BorderStyle.solid,
                          width: setRatio(1)))),
              child: ITextField(
                inputText:
                    "${date.year.toString()}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}",
                showSuffix: true,
                enabled: false,
                inputBorder: InputBorder.none,
                keyboardType: ITextInputType.text,
                hintText: '输入生日',
                hintStyle: TextStyle(color: Color(0xFFC2C2C2)),
                textStyle: TextStyle(color: Color(0xFF333333)),
                deleteIcon: Icon(
                  IconFont.DOWN_ARROW,
                  size: setRatio(7),
                  color: Color(0xff323333),
                ),
              ),
            ),
          );
        });
  }

  Widget _buildBottomPicker(Widget picker) {
    return Container(
      height: setRatio(216),
      padding: const EdgeInsets.only(top: 6.0),
      color: CupertinoColors.white,
      child: DefaultTextStyle(
        style: const TextStyle(
          color: CupertinoColors.black,
          fontSize: 22.0,
        ),
        child: GestureDetector(
          onTap: () {},
          child: SafeArea(
            top: false,
            child: picker,
          ),
        ),
      ),
    );
  }
}
